﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace ArgusLib.Controls
{
	public class ComboBox : System.Windows.Forms.ComboBox
	{
		public ComboBox()
			: base()
		{
			this.BackColor = Color.Black;
			this.ForeColor = Color.White;
			this.AutoSizeDropDownWidth = false;
		}

		public bool AutoSizeDropDownWidth { get; set; }

		protected override void OnLayout(System.Windows.Forms.LayoutEventArgs levent)
		{
			if (this.AutoSizeDropDownWidth == true)
			{
				int width = 0;
				using (Graphics g = this.CreateGraphics())
				{
					for (int i = 0; i < this.Items.Count; i++)
					{
						MeasureItemEventArgs e = new MeasureItemEventArgs(g, i);
						this.OnMeasureItem(e);
						if (e.ItemWidth > width)
							width = e.ItemWidth;
					}
				}
				if (width > 0)
				{
					if (this.Items.Count > this.MaxDropDownItems)
						width += SystemInformation.VerticalScrollBarWidth;
					this.DropDownWidth = width;
				}
			}
			base.OnLayout(levent);
		}

		protected override void OnMeasureItem(System.Windows.Forms.MeasureItemEventArgs e)
		{
			ListControlConvertEventArgs fe = new ListControlConvertEventArgs(this.Items[e.Index], typeof(string), this.Items[e.Index]);
			this.OnFormat(fe);
			string text = fe.Value as string;
			if (text != null)
				e.ItemWidth = (int)e.Graphics.MeasureString(text, this.Font).Width;
			base.OnMeasureItem(e);
		}
	}
}
